﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Windows.Forms;
using GSQ.BusinessEntities;
using GSQ.BusinessLogic;
using UIapp.Interfaces;

namespace UIapp.Vistas
{
    public partial class frmClientes : Plantillas.frmViewPlantilla, IPantalla
    {
        public frmClientes(bool asView)
        {
            InitializeComponent();
            AsView = asView;
            this.entityCollection = new List<CLIENTES>();
        }

        #region Variables
        List<CLIENTES> entityCollection;
        bool AsView;
        #endregion

        #region Implementacion IPantalla
        public int ComandosSoportados
        {
            get
            {
                return (int)ComandosPantalla.Buscar |
                    (int)ComandosPantalla.Modificar |
                    (int)ComandosPantalla.Nuevo;
            }
        }

        public void Nuevo()
        {
            Dialogos.frmClientes frm = new Dialogos.frmClientes(null);
            frm.MdiParent = this.MdiParent;
            frm.Show();
        }

        public void Modificar()
        {
            if (this.bindingSource.Count > 0)
            {
                Dialogos.frmClientes frm = new Dialogos.frmClientes((CLIENTES)this.radGridView.CurrentRow.DataBoundItem);
                frm.MdiParent = this.MdiParent;
                frm.Show();
            }
        }

        public void Eliminar()
        {
            throw new NotImplementedException();
        }

        public void Refrescar()
        {
            this.Buscar();
        }

        public void Buscar()
        {
            this.backgroundWorker.RunWorkerAsync();
            Comun.ShowLoadingFRM();
        }

        public void AgregarProducto(Controles.TransferEntity tranferEntity)
        {
            throw new NotImplementedException();
        }
        #endregion

        #region Tratamiento de eventos
        private void txtBuscar_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                this.Buscar();
                e.Handled = true;
            }
        }

        private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            this.Listar();
        }

        private void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            Comun.CloseLoadingFRM();
            if (e.Error != null)
                Comun.ShowMessage(e.Error);
            else
                this.bindingSource.DataSource = this.entityCollection.ToList();
        }

        private void radGridView_CellDoubleClick(object sender, Telerik.WinControls.UI.GridViewCellEventArgs e)
        {
            if (AsView && this.entityCollection.Count > 0 && ((CLIENTES)this.radGridView.CurrentRow.DataBoundItem).ACTIVO)
            {
                this.DialogResult = System.Windows.Forms.DialogResult.OK;
                this.Close();
            }
        }
        #endregion

        #region Metodos auxiliares
        public void Listar()
        {
            this.entityCollection.Clear();
            decimal id = 0;
            if (this.txtSearchValue.Text.Trim() == string.Empty)
            {
                this.entityCollection = new ClientesBL().GetAll();
                return;
            }
            if (this.chkPorSuplidor.IsChecked)//Buscar por sting cliente o por código
            {
                if (decimal.TryParse(this.txtSearchValue.Text.Trim(), out id))
                {
                    this.entityCollection.Add(new ClientesBL().GetById(id));
                }
                else
                {
                    this.entityCollection = new ClientesBL().GetByCliente(this.txtSearchValue.Text.Trim());
                }
            }
            else if (this.chkRnc.IsChecked)//Buscar por sting rnc
            {
                this.entityCollection.Add(new ClientesBL().GetByRnc(this.txtSearchValue.Text.Trim()));
            }
            else if (this.chkCiudad.IsChecked)//Buscar por sting suplidor o por código
            {
                if (decimal.TryParse(this.txtSearchValue.Text.Trim(), out id))
                {
                    this.entityCollection = new ClientesBL().GetByCiudad(id);
                }
                else
                {
                    this.entityCollection = new ClientesBL().GetByCiudad(this.txtSearchValue.Text.Trim());
                }
            }
        }
        #endregion
    }
}
